<template>
<div id="cont">
  <div class="s-main">
  	<form enctype="multipart/form-data" method="post">
			<input type="text" v-model="msg">
  	  <input type="file" name="avatars" id="avatars" @change="upload">
  	</form>
  </div>
</div>
</template>

<script>
import app from '@/utils/index.js';
import { mapActions } from 'vuex';

export default {
	name: 'upload',
	data() {
		return {
			msg: ''
		}
	},
	methods: {
		...mapActions([
			'SET_UINFO'
		]),
		upload(e) {
			let files = e.target.files || e.dataTransfer.files,
				imgSrc;
			var formdata = new FormData();
			if(!files.length) {
				return false;
			}
			// let params = new Object(formdata);
			console.log('34...............', files[0]);
			formdata.append("avatars", files[0]);
			formdata.append("name", this.msg);
			// params.avatars = files[0];
			// params.name = this.msg;
			
			app.request({
				url: '/api/updateAvatars',
				method: 'POST',
				headers: {'Content-Type': 'application/x-www-form-urlencoded'},
				data: formdata,
				success: res => {
					console.log(res)
					const data = res.data;
					if(data.s) {
						this.$store.commit('SET_UINFO', data.d);
					}
				}
			})
		  // axios.post('/api/updateAvatars',
		  // 	formdata).then((response) => {
		  // 	let data = response.data;
		  // 	if(data.s) {
		  // 	  this.$store.commit('SET_AVATARS', data.d);
		  //     flash('success', '更新成功！');
		  // 	}
		  // })
		}
  }
}
</script>